"""
    1.打开浏览器，访问登陆界面
    2.登录
    3.添加用户
"""
import ddt
import unittest
# import pymysql as pymysql
# from parameterized import parameterized
from selenium import webdriver
from selenium.webdriver.common.by import By
import time


# self代表当前类
@ddt.ddt()
class customerAdd(unittest.TestCase):

    def setUp(self) -> None:
        self.wd = webdriver.Chrome()
        self.wd.maximize_window()
        self.wd.get("http://127.0.0.1/mgr/sign.html")
        self.wd.implicitly_wait(3)

        # # 定位到”用户名“,传入username
        # self.wd.find_element(By.ID, "username").send_keys("byhy")
        # # 定位到”密码“，传入pwd
        # self.wd.find_element(By.ID, "password").send_keys(88888888)
        # # 点击登录
        # self.wd.find_element(By.XPATH, "/html/body/div/div[2]/div[1]/div[3]/div/button").click()
        # time.sleep(2)

    # 使用参数化
    # 状态码   成功1 失败0
    # data = [
    #     ('by','88888888','用户名或密码错误','0'),
    #     ('byhy','123','用户名或密码错误','0'),
    #     ('','88888888','请输入用户名','0'),
    #     ('byhy','','请输入密码','0'),
    #     ('byhy','88888888','白云销售管理系统','1')
    # ]
    # @parameterized.expand(data)
    @ddt.file_data("loginTestCase.json")
    def test_login(self,status,z,username,pwd):
        # 定位到”用户名“,传入username
        self.wd.find_element(By.ID, "username").send_keys(username)
        # 定位到”密码“，传入pwd
        self.wd.find_element(By.ID, "password").send_keys(pwd)
        # 点击登录
        self.wd.find_element(By.XPATH, "/html/body/div/div[2]/div[1]/div[3]/div/button").click()

        # 登录成功
        if status == '1':
            try:
                result = self.wd.title
                self.assetEqual(z, result)
            except  AssertionError as E:
                print("断言失败")
                raise E
        # 登录失败
        if status == '0':
            try:
                ele = self.wd.switch_to.alert
                msg = ele.text
                self.assetEqual(z, msg)
            except  AssertionError as E:
                print("断言失败")
                raise E

    # 添加客户
    # @ddt.file_data("aTesta.json")
    # def test_cus_add(self, username, phone, z,a):
    #     # 定位”添加客户“按钮
    #     self.wd.find_element(By.XPATH, '//*[@id="root"]/div/section[2]/div[1]/button').click()
    #     self.wd.find_element(By.XPATH, '//*[@id="root"]/div/section[2]/div[1]/div[1]/div[1]/input').send_keys(username)
    #     self.wd.find_element(By.XPATH, '//*[@id="root"]/div/section[2]/div[1]/div[1]/div[2]/input').send_keys(phone)
    #     # self.wd.find_element(By.XPATH, '//*[@id="root"]/div/section[2]/div[1]/div[1]/div[3]/textarea').send_keys()
    #     self.wd.find_element(By.XPATH, '//*[@id="root"]/div/section[2]/div[1]/div[2]/button[1]').click()
    #
    #     if a == 0:
    #         try:
    #             result = self.wd.switch_to.alert
    #             aa=result.text
    #             self.assertIn(z,aa)
    #             result.accept()
    #         except  AssertionError as E:
    #             print("断言失败")
    #             raise E
    #     # 添加失败
    #     if a == 1:
    #         try:
    #             ele = self.wd.switch_to.alert
    #             msg = ele.text
    #             self.assertEqual(z, msg)
    #         except  AssertionError as E:
    #             print("断言失败")
    #             raise E

    # 关闭浏览器
    # def tearDown(self) -> None:
    #     self.wd.quit()


if __name__ == '__main__':
    unittest.main()
